/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：CycleQueryScreen.java <br>
 * 创建时间：2015年4月28日 下午9:54:01 <br>
 * 所属项目：GmudEX <br>
 * @author 12548
 */
package lostland.gmud.exv2.battle.netbattle;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;

import lostland.general.network.GmudHttp;
import lostland.general.network.GmudRequestListener;
import lostland.general.network.GmudRestUrl;
import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;

/**
 * 类名：CycleQueryScreen <br>
 * 说明：轮询网络信息。。。
 *
 * @author 12548
 */
public class CycleQueryScreen extends CScreen {

	public interface CycleQuery {
		boolean ok(String info);

		void onOK(String info);

		void onTimeOut();

		void onError();
	}

	CycleQuery query;

	GmudRestUrl url;
	RequestParams rp;


	/**
	 * @param query
	 * @param url
	 * @param rp
	 * @param interval 轮询间隔
	 * @param times    轮询最大次数
	 * @param msg      显示的消息
	 */
	public CycleQueryScreen(GmudRestUrl url, RequestParams rp, int interval, int times, String msg, CycleQuery query) {
		super();
		this.query = query;
		this.url = url;
		this.rp = rp;
		this.interval = interval;
		this.times = times;
		this.msg = msg;
	}

	public int interval = 5;

	int times = 5;

	public String msg = "正在等待服务器消息……";

	public CScreen drawing_bg = BattleScreen.getInstance();


	private volatile float currentTime;
	private volatile boolean querying = false;

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#present(float)
	 */
	@Override
	public void present(float deltaTime) {
		drawing_bg.present(0);
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();
		g.drawRect(64, 98, 192, 96, Gmud.getBgColor());
		g.drawText(String.format("将在%d秒内重新查询服务器信息……", (int) (currentTime)), 64, 98, FontSize.FT_16PX, 190);
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#pause()
	 */
	@Override
	public void pause() {
		// TODO 自动生成的方法存根

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#resume()
	 */
	@Override
	public void resume() {
		// TODO 自动生成的方法存根

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.CScreen#dispose()
	 */
	@Override
	public void dispose() {
		// TODO 自动生成的方法存根

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.blgframework.BasicScreen#update(float)
	 */
	@Override
	public void update(float deltaTime) {
		if (querying)
			return;
		if (currentTime <= 0) {

			currentTime = interval;
			querying = true;
			new GmudHttp(msg).postLocked(url, rp, new GmudRequestListener() {

				@Override
				public void onSucc(ResponseInfo<String> info) {
					boolean ok = query.ok(info.result);
					if (ok) {
						CycleQueryScreen.this.removeFromGame();
						query.onOK(info.result);
					} else {
						times--;
						if (times <= 0) {
							CycleQueryScreen.this.removeFromGame();
							query.onTimeOut();
						}
					}
				}

				@Override
				public void onFail(HttpException error, String msg) {
					CycleQueryScreen.this.removeFromGame();
					query.onError();
				}
			});
			;
		} else {
			currentTime -= deltaTime;
		}
	}

}
